package fr.iut.tp3.view.components;

import java.awt.BorderLayout;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.TableColumn;

import fr.iut.tp3.Context;
import fr.iut.tp3.controller.commands.CSupprimerComboboxTab;
import fr.iut.tp3.controller.commands.Commande;
import fr.iut.tp3.view.listeners.ComboboxTabBtnListener;

public class ComboxTableUtilisateurs extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3963612509033278218L;

	private JTable table;
	private JButton supprimerButton;
	private Context context;
	private JScrollPane scrollPane;

	public ComboxTableUtilisateurs(Context context) {
		super();
		this.context = context;
		this.setLayout(new BorderLayout());
		table = new JTable(context.getListeUtilisateurs());
		scrollPane = new JScrollPane(table,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		
		add(scrollPane, BorderLayout.CENTER);
		TableColumn columnPrivilege = table.getColumnModel().getColumn(2);

		JComboBox<String> comboBox = new JComboBox<String>();
		comboBox.addItem("Admin");
		comboBox.addItem("Utilisateur");
		comboBox.addItem("Invité");
		columnPrivilege.setCellEditor(new DefaultCellEditor(comboBox));

		supprimerButton = new JButton("Supprimer ligne");
		add(supprimerButton, BorderLayout.SOUTH);
		addListeners();
	}

	public JTable getTable() {
		return table;
	}

	public JButton getSupprimer() {
		return supprimerButton;
	}

	/**
	 * Ajout des listeners.
	 */
	private void addListeners() {
		// Les commandes.
		Commande supprimer = new CSupprimerComboboxTab(context, table);

		// Définir les actions.
		ComboboxTabBtnListener listener = new ComboboxTabBtnListener(supprimer);
		supprimerButton.setActionCommand("supprimer");

		// Ajout des listeners.
		supprimerButton.addActionListener(listener);

	}

}
